"""
@Time ： 2021/11/23 15:31
@Auth ： ChenZhangKun
"""
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By

import Consistant
from 期房买卖抵 import Login


class Buy_House(Login):
    def __init__(self, chormePath, url):
        self.chormePath = chormePath
        self.url = url

    def openUrl(self):
        """
        打开网页
        :return:
        """
        self.webDriver = webdriver.Chrome(self.chormePath)
        self.webDriver.implicitly_wait(Consistant.TEN)
        self.webDriver.get(self.url)

    def second_scan_code(self):
        # 登录
        self.login("15723315609", "Czk1994#")
        # 切换角色
        self.click_main()
        self.change_danwei_button()
        self.change_person_role(Consistant.FIRST)
        # 点击不查看房屋
        self.click_no()
        # 扫码
        self.saomiao_code("/一体化/code2.png")
        time.sleep(4)
        self.change_tag("买方物权")
        time.sleep(2)
        # 选择共有情况
        self.select_common_have("单独所有")
        # 点击同意
        time.sleep(2)
        self.click_allow_or_not("同意")
        time.sleep(2)
        # 截图
        self.screen_cut("code3.png")
        # 返回
        self.return_main()
        time.sleep(2)
        # 扫码
        self.saomiao_code("/一体化/code3.png")
        # 签名
        time.sleep(2)
        self.sign()
    def click_allow_or_not(self, txt):
        """
        点击同意或不同意
        :param txt:
        :return:
        """
        elements = self.webDriver.find_elements(By.CLASS_NAME, "v-btn__content")
        time.sleep(1)
        for el in elements:
            if el.text == txt:
                el.click()

    def select_common_have(self, txt):
        """
        选择共有情况
        :param txt:
        :return:
        """
        elements=self.webDriver.find_elements(By.CLASS_NAME,"v-select__selections")
        time.sleep(3)
        elements[9].click()
        time.sleep(2)
        elements=self.webDriver.find_elements(By.CLASS_NAME,"v-list-item__title")
        time.sleep(1)
        for el in elements:
            if el.text==txt:
                el.click()

    def test_buy(self):
        # 登录
        self.login("15723315609", "Czk1994#")
        # 切换角色
        self.click_main()
        self.change_danwei_button()
        self.change_person_role(Consistant.FIRST)
        # 点击不查看房屋
        self.click_no()
        # 扫码
        self.saomiao_code("/一体化/code1.png")
        # 检查是否需要上传身份证
        time.sleep(2)
        flag = self.check_is_need_upload_idCard()
        if flag:
            # 上传身份证
            self.upload_idCard(r'D:\陈彰坤.jpg', r'D:\陈彰坤.jpg')
        self.sign()

    def sign(self):
        """
        签名
        :return:
        """
        time.sleep(3)
        element = self.webDriver.find_element(By.CLASS_NAME, "canvasWrap")
        ac = ActionChains(self.webDriver)
        # 长按左键
        ac.click_and_hold(element).move_by_offset(20, 0).perform()
        time.sleep(2)
        # 释放
        ac.release(element)
        # 选择确认
        elements = self.webDriver.find_elements(By.TAG_NAME, "span")
        for el in elements:
            if el.text == '确认':
                el.click()
        # 点击知道了
        self.click_know()

    def check_is_need_upload_idCard(self):
        """
        判断是否需要上传
        :return:
        """
        flag = False
        try:
            elements = self.webDriver.find_elements(By.CLASS_NAME, "btn")
            for el in elements:
                if el.text == "获取电子证照":
                    flag = True
        except Exception as ex:
            print(ex)
            flag = False
        return flag

    def upload_idCard(self, path1, path2):
        """
        上传身份证
        :param path1:
        :param path2:
        :return:
        """
        time.sleep(2)
        elements = self.webDriver.find_elements(By.TAG_NAME, "img")
        # 点击
        elements[Consistant.SECOND].click()
        time.sleep(2)
        element = self.webDriver.find_element(By.CSS_SELECTOR, "input[type='file']")
        time.sleep(2)
        # 上传图片
        element.send_keys(path1)
        time.sleep(4)
        # 再次上传
        elements = self.webDriver.find_elements(By.CSS_SELECTOR, "div[class=upload] img")
        elements[Consistant.SECOND].click()
        element = self.webDriver.find_element(By.CSS_SELECTOR, "input[type='file']")
        time.sleep(2)
        element.send_keys(path2)
        # 点击下一步
        time.sleep(2)
        elements = self.webDriver.find_elements(By.CLASS_NAME, "btn")
        for el in elements:
            if el.text == "下一步":
                el.click()
                break

    def click_no(self):
        """
        点击不查看
        :return:
        """
        flag = self.check_isExist("class name", "btn")
        if flag:
            noEle = self.webDriver.find_element(By.CLASS_NAME, "btn")
            print(noEle.text)
            noEle.click()

    def saomiao_code(self, path):
        """
        扫描
        :param path:
        :return:
        """
        time.sleep(3)
        element = self.webDriver.find_element(By.CSS_SELECTOR, "input[type='file']")
        time.sleep(2)
        element.send_keys(path)


if __name__ == '__main__':
    buy = Buy_House(r'D:\sofwares\chrom\chromedriver.exe', "http://yancheng.bit-service.com:8082/ii/#/login")
    buy.openUrl()
    buy.test_buy()
